package com.ely.coupon.common.idempotent.config;

import com.ely.coupon.common.idempotent.interceptor.IdempotentTokenInterceptor;
import com.ely.coupon.common.idempotent.service.TokenService;
import com.ely.coupon.common.idempotent.service.impl.TokenServiceImpl;
import lombok.Setter;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

@Configuration
@EnableConfigurationProperties({IdempotentProperties.class})
@Setter
public class IdempotentConfig implements WebMvcConfigurer {
    @Resource
    private IdempotentTokenInterceptor interceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(interceptor).addPathPatterns("/**");
    }


    @Bean
    public IdempotentTokenInterceptor interceptor() {
        return new IdempotentTokenInterceptor();
    }

    @Bean
    public TokenService tokenService() {
        return new TokenServiceImpl();
    }


}
